﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BE.Model;

namespace BE
{
    public static class WCFHelper
    {
        private static Dictionary<int, Room> RoomCache = new Dictionary<int, Room>();
        private static Dictionary<int, Dorm> DormCache = new Dictionary<int, Dorm>();
        private static Dictionary<int, Charge> ChargeCache = new Dictionary<int, Charge>();
        private static Dictionary<int, Profession> ProfessionCache = new Dictionary<int, Profession>();
        private static Dictionary<int, Student> StudentCache = new Dictionary<int, Student>();
        private static Dictionary<int, Program> ProgramCache = new Dictionary<int, Program>();
        private static Dictionary<int, City> CityCache = new Dictionary<int, City>();

        public static Room TransferRoom(Room room)
        {
            if (room == null)
            {
                return null;
            }

            Room retRoom = new Room();

            retRoom.ID = room.ID;
            retRoom.Floor = room.Floor;
            retRoom.Number = room.Number;
            retRoom.NumberOfBeds = room.NumberOfBeds;
            retRoom.DormID = room.DormID;
            retRoom.Dorm = TransferDorm(room.Dorm);
            retRoom.ChargeID = room.ChargeID;
            retRoom.Charge = TransferCharge(room.Charge);
                      
            return retRoom;
        }

        public static Placement TransferPlacement(Placement placement)
        {
            if (placement == null)
            {
                return null;
            }

            Placement retPlacement = new Placement();

            retPlacement.ID = placement.ID;
            retPlacement.RoomID = placement.RoomID;
            retPlacement.StudentID = placement.StudentID;
            retPlacement.ChargeID = placement.ChargeID;
            retPlacement.ChargeEnd = placement.ChargeEnd;
            retPlacement.ChargeStart = placement.ChargeStart;
            retPlacement.Comments = placement.Comments;
            retPlacement.Departure = placement.Departure;
            retPlacement.Enterance = placement.Enterance;
            retPlacement.Student = TransferStudent(placement.Student);
            retPlacement.Room = TransferRoom(placement.Room);
            retPlacement.Charge = TransferCharge(placement.Charge);

            return retPlacement;
        }

        public static Student TransferStudent(Student student)
        {
            if (student == null)
            {
                return null;
            }

            Student retStudent = new Student();

            retStudent.ID = student.ID;
            retStudent.FirstName = student.FirstName;
            retStudent.LastName = student.LastName;
            retStudent.Email = student.Email;
            retStudent.PhoneNumber = student.PhoneNumber;
            retStudent.CityID = student.CityID;
            retStudent.City = TransferCity(student.City);
            retStudent.ProfessionID = student.ProfessionID;
            retStudent.Profession = TransferProfession(student.Profession); 
            retStudent.ProgramID = student.ProgramID;
            retStudent.Program = TransferProgram(student.Program);
            retStudent.Comments = student.Comments;

            return retStudent;
        }
        public static Profession TransferProfession(Profession profession)
        {
            if (profession == null)
            {
                return null;
            }
            if (!ProfessionCache.ContainsKey(profession.ID))
            {
                ProfessionCache[profession.ID] = new Profession();
                ProfessionCache[profession.ID].ID = profession.ID;
                ProfessionCache[profession.ID].Name = profession.Name;
            }
            return ProfessionCache[profession.ID];
        }
        public static City TransferCity(City city)
        {
            if (city == null)
            {
                return null;
            }
            if (!CityCache.ContainsKey(city.ID))
            {
                CityCache[city.ID] = new City();
                CityCache[city.ID].ID = city.ID;
                CityCache[city.ID].Name = city.Name;
            }
            return CityCache[city.ID];
        }
        public static Program TransferProgram(Program program)
        {
            if (program == null)
            {
                return null;
            }
            if (!ProgramCache.ContainsKey(program.ID))
            {
                ProgramCache[program.ID] = new Program();
                ProgramCache[program.ID].ID = program.ID;
                ProgramCache[program.ID].Name = program.Name;
            }
            return ProgramCache[program.ID];
        }


        public static Dorm TransferDorm(Dorm dorm)
        {
            if (dorm == null)
            {
                return null;
            }
            if (!DormCache.ContainsKey(dorm.ID))
            {
                DormCache[dorm.ID] = new Dorm();
                DormCache[dorm.ID].ID = dorm.ID;
                DormCache[dorm.ID].Name = dorm.Name;
                DormCache[dorm.ID].NumberOfFloors = dorm.NumberOfFloors;
            }
            return DormCache[dorm.ID];
        }

        public static Charge TransferCharge(Charge charge)
        {
            if (charge == null)
            {
                return null;
            }
            if (!DormCache.ContainsKey(charge.ID))
            {
                ChargeCache[charge.ID] = new Charge();
                ChargeCache[charge.ID].ID = charge.ID;
                ChargeCache[charge.ID].Name = charge.Name;
                ChargeCache[charge.ID].Code = charge.Code;
                ChargeCache[charge.ID].Amount = charge.Amount;
            }
            return ChargeCache[charge.ID];
        }
    }
}
